<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$storeid=$this->model('store')->checklogin();
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid'],'store_id'=>$storeid);
	if(!empty($_RQ['ordersn'])){
		$params['ordersn']=trim($_RQ['ordersn']);
		$return['params']['ordersn']=$_RQ['ordersn'];
	}
	if(!empty($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	if(!empty($_RQ['name'])){
		$params['name like']='%'.trim($_RQ['name']).'%';
		$return['params']['name']=$_RQ['name'];
	}
    $fields=array('id','ordersn','name','mobile','worker_id','store_id','mid','createtime','status','price','type','day','time');
    $orderby='createtime desc';
	$total='';
	$return['list']=pdo_getslice('jy_mapfuwu_order',$params,array($pindex,$psize),$total,$fields,'',$orderby);	
	foreach($return['list'] as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			if($item['cuid'] && $item['cuid']!=$_SESSION['uid']){
				$item['cel']=1;
			}
			if($item['worker_id']){
				$item['worker']=pdo_get('jy_mapfuwu_worker',array('id'=>$item['worker_id']),array('name','mobile','sex','avatar','mid'));
				$item['worker']['ordersum']=pdo_count('jy_mapfuwu_order',array('uniacid'=>$_SESSION['uniacid'],'status'=>array(5,9),'worker_id'=>$item['worker_id']),'id');
				$item['worker']['avatar']=tomedia($item['worker']['avatar']);
			}
			$item['goods']=json_decode($item['goods'],true);
			$item['user']=pdo_get('jy_mapfuwu_member',array('mid'=>$item['mid']),array('name','mobile'));
	}
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'订单管理'),
		),
		'list'=>array('data'=>'list','edit'=>false,'list'=>array(
			array('name'=>'member','title'=>'会员信息_Member','type'=>'member'),
			array('name'=>'ordersn','title'=>'订单号','type'=>'string'),
			//array('name'=>'store_id','title'=>'网点','type'=>'select','options'=>'stores'),
			//array('name'=>'worker_id','title'=>'师傅','type'=>'select','options'=>'workers','option_title'=>'name'),
			array('name'=>'type','title'=>'类型','type'=>'radio','options'=>'type'),
			array('name'=>'user.name','title'=>'联系人','type'=>'string'),
			array('name'=>'user.mobile','title'=>'电话','type'=>'string'),
			array('name'=>'price','title'=>'金额','type'=>'string'),
			array('name'=>'status','title'=>'订单状态','type'=>'radio','options'=>'status'),	
			array('name'=>'createtime','title'=>'下单时间','type'=>'time'),			
		),
		'action'=>array(
			//array('type'=>'modal','op'=>'setworker','title'=>'指派师傅'),
			array('type'=>'href','op'=>'detail','title'=>'详细'),
		),),
		'search'=>array(
			array('name'=>'ordersn','width'=>15,'title'=>'订单号','type'=>'string'),
			array('name'=>'name','width'=>15,'title'=>'联系人','type'=>'string'),
			array('name'=>'status','width'=>15,'title'=>'订单状态','type'=>'radio','options'=>'status'),
		)
	);
	//$return['stores']=pdo_getall('jy_mapfuwu_store',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	//$return['workers']=pdo_getall('jy_mapfuwu_worker',array('uniacid'=>$_SESSION['uniacid']),array('id','name'),'id');
	$return['status']=$this->order_status;
	$return['type']=array('立马维修','预约维修');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='setworker'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$worker_id=intval($post['worker_id']);
		pdo_update('jy_mapfuwu_order',array('worker_id'=>$worker_id),array('id'=>intval($post['id']),'uniacid'=>$_SESSION['uniacid']));
		exi('指派成功！','','list');
	}
	$item=$this->get('order',array('uniacid'=>$_SESSION['uniacid'],'id'=>intval($_RQ['id'])));
	if(!$item){
		exi('指定订单不存在！','error');
	}
	if(!$item['store_id']){
		exi('请先指派网点！','error');
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'指派师傅',
		'edit'=>array(
			array('name'=>'worker_id','title'=>'师傅','type'=>'select','options'=>'workers','option_title'=>'name'),
		),
	);
	$return['workers']=pdo_getall('jy_mapfuwu_worker',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>$item['store_id']),array('id','name'),'id');
	exi($return);
}
if($_RQ['op']=='del'){
	exi('网点不能自己删除订单！','error');
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_mapfuwu_order',array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_mapfuwu_order',array('id'=>$id));	
			}
		}
	}
	exi('删除成功！');

}
if($_RQ['op']=='detail'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择订单','error','list');
	}
	
	$item=pdo_get('jy_mapfuwu_order',array('id'=>$id,'store_id'=>$storeid));
	$item['createtime']=date('Y-m-d H:i:s',$item['createtime']);
	$item['member']=cfc('core')->member_show($item['mid']);
	if($item['cuid'] && $item['cuid']!=$_SESSION['uid']){
		$item['cel']=1;
	}
	$item['store']=pdo_get('jy_mapfuwu_store',array('id'=>$item['store_id']),array('name','mobile','image','mid','address','pcd'));
	$item['store']['image']=tomedia($item['store']['image']);
	$item['store']['pcd']=json_decode($item['store']['pcd'],true);
	$item['worker']=pdo_get('jy_mapfuwu_worker',array('id'=>$item['worker_id']),array('name','mobile','sex','avatar','mid'));
	$item['worker']['ordersum']=pdo_count('jy_mapfuwu_order',array('uniacid'=>$_SESSION['uniacid'],'status'=>array(5,9),'worker_id'=>$item['worker_id']),'id');
	$item['worker']['avatar']=tomedia($item['worker']['avatar']);
	$item['goods']=json_decode($item['goods'],true);
	$item['user']=pdo_get('jy_mapfuwu_member',array('mid'=>$item['mid']),array('name','mobile'));
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'订单详情',		
		'edit'=>array(
			array('name'=>'ordersn','title'=>'订单号','type'=>'string'),
			array('name'=>'type','title'=>'联系人','type'=>'radio','options'=>'type'),
			array('name'=>'user.name','title'=>'联系人','type'=>'string'),
			array('name'=>'user.mobile','title'=>'电话','type'=>'string'),
			array('name'=>'price','title'=>'金额','type'=>'string'),
			array('name'=>'status','title'=>'订单状态','type'=>'radio','options'=>'status'),	
			array('name'=>'createtime','title'=>'下单时间','type'=>'time'),
			
		)
	);
	$return['htmls']=$htmls;
	$return['type']=array('立马维修','预约维修');
	$return['status']=$this->order_status;
	exi($return);
}
?>